package ManageGameHServer.worker;

import ManageGameHServer.classFile.*;
import ManageGameHServer.code.WebWorker;
import ManageGameHServer.conf.Setup;
import org.json.JSONObject;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class hthome extends WebWorker {

    @Override
    public String execute() throws Exception {
        if(this.POST.containsKey("name") && this.POST.containsKey("password")){                            //验证用户名和密码

            String name=this.POST.get("name");
            String password=this.POST.get("password");
            JSONObject rData=new JSONObject();
            try {
                Connection con =DBConnectO.getCon(2);
                try {
                    PreparedStatement ps =con.prepareStatement("select id,userName,password from user_list where userName=? and password =?");
                    ps.setString(1,name);
                    ps.setString(2, Mmd5.createString(password));
                    ResultSet resultSet = ps.executeQuery();
                    resultSet.last();int row =resultSet.getRow();resultSet.first();
                    try {
                        if(row==1){
                            this.sessionData.put("isLogin",1);          //判断已经登入
                            this.sessionData.put("uid",resultSet.getString("id"));
                            this.sessionData.put("userName",resultSet.getString("userName"));
                            this.saveSessionWorker();                       //保存一下
                            rData.put("state",1);
                            rData.put("msg","登入成功");
                        }else {
                            rData.put("state",0);
                            rData.put("msg","没有该用户。");
                        }
                    }catch (Exception e){
                        rData.put("state",0);
                        rData.put("msg","参数错误");
                        Log.errorLog(e.getMessage());
                    }finally {
                        resultSet.close();
                    }
                }catch (Exception e){
                    rData.put("state",0);
                    rData.put("msg","参数错误");
                    Log.errorLog(e.getMessage());
                }finally {
                    DBConnectO.redCon(2,con);
                }
            }catch (Exception e){
                rData.put("state",0);
                rData.put("msg","参数错误");
                Log.errorLog(e.getMessage());
            }
            return rData.toString();
        }else {
            MSmarty mSmarty = new MSmarty(Setup.homeUrl + "/views/html/hthome.html");
            return mSmarty.display();
        }
    }
}
